package com.hfzy.ihk.web.linkcall.utils;

import com.hfzy.ihk.common.util.string.StringUtil;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**IK分词器工具类
 * @Auther: Administrator
 * @Date: 2018/10/12 11:20
 * @Description:
 */
public class IKSegmenterUtils {

    private static IKSegmenterUtils iKSegmenterUtils;

    private IKSegmenterUtils() {
    }

    public static  IKSegmenterUtils getInstance(){

        if (iKSegmenterUtils==null){

            synchronized (IKSegmenterUtils.class){

                if (iKSegmenterUtils==null){

                    iKSegmenterUtils=new IKSegmenterUtils();
                }

            }
        }

        return iKSegmenterUtils;
    }


    public List<String> getKeywords(String[] text){

            List<String> keywords=new ArrayList<>();

             Arrays.stream(text).forEach((question)->{

            StringReader reader=new StringReader(question);

            IKSegmenter ik = new IKSegmenter(reader,true);

            Lexeme lex = null;

                 try {
                     while((lex=ik.next())!=null){
                         keywords.add(lex.getLexemeText());
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }

             });

             return keywords.stream().distinct().filter(s -> {return StringUtil.isNotNull(s);}).collect(Collectors.toList());
    }
}
